package com.sdkj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sdkj.mapper.*;
import com.sdkj.pojo.*;
import com.sdkj.service.ConsumerService;
import com.sdkj.util.DateUtils;
import com.sdkj.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ConsumerServiceImpl implements ConsumerService {

    @Resource
    private SetmealMapper setmealMapper;

    @Resource
    private CheckgroupMapper checkgroupMapper;

    @Resource
    private CheckitemMapper checkitemMapper;

    @Resource
    private OrdersettingMapper ordersettingMapper;

    @Resource
    private MemberMapper memberMapper;

    @Resource
    private OrdersMapper ordersMapper;

    //展示所有套餐
    @Override
    public List<Setmeal> showSetmealInfo() {
        return setmealMapper.selectList(null);
    }

    //展示套餐详情
    @Override
    public Setmeal showSetmealDetail(Integer id) {
        //获取套餐信息
        Setmeal setmeal = setmealMapper.selectById(id);
        //套餐包含的检查组信息
        List<Checkgroup> groupBySid = checkgroupMapper.getGroupBySid(id);
        //获取每个检查组里面包含的检查项信息
        for (Checkgroup checkgroup : groupBySid) {
            List<Checkitem> itemByGid = checkitemMapper.getItemByGid(checkgroup.getId());
            checkgroup.setCheckItems(itemByGid);
        }
        setmeal.setCheckGroups(groupBySid);
        return setmeal;
    }


    //提交预约的方法
    @Override
    public Result submitOrder(Map map) {
        Integer oid = 0;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try{
            //判断 该日期是否已经设定了预约信息
            String  orderDateStr = (String)map.get("orderDate");
            Date date = simpleDateFormat.parse(orderDateStr);
            LambdaQueryWrapper<Ordersetting> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Ordersetting::getOrderdate,date);
            Ordersetting ordersetting = ordersettingMapper.selectOne(lambdaQueryWrapper);
            if (ordersetting ==null){
                //该日期没有设定预约信息
                return new Result(false,"该日期没有开放预约");
            }else{
                //改日开放了预约信息
                Integer number = ordersetting.getNumber();
                Integer reservations = ordersetting.getReservations();
                //使用equals方法比较数值的大小
                if (number.equals(reservations)){
                    return  new Result(false,"该日期已经约满,从新选择日期");
                }else{
                    //设定预约信息  该日期没有约满
                    //体检人是否已经预约过  身份证号
                    String idCardStr  = (String)map.get("idCard");
                    LambdaQueryWrapper<Member> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper1.eq(Member::getIdcard,idCardStr);
                    Member member = memberMapper.selectOne(lambdaQueryWrapper1);
                    //套餐id
                    String setmealIdStr = (String) map.get("setmealId");
                    int setmealId = Integer.parseInt(setmealIdStr);
                    if (member !=null){
                        //查到体检人
                        LambdaQueryWrapper<Orders> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
                        lambdaQueryWrapper2.eq(Orders::getOrderdate,date);
                        lambdaQueryWrapper2.eq(Orders::getSetmealid,setmealId);
                        lambdaQueryWrapper2.eq(Orders::getMemberid,member.getId());
                        Orders orders = ordersMapper.selectOne(lambdaQueryWrapper2);
                        if (orders != null){
                            return new Result(false,"不能重复预约");
                        }
                    }else{
                        //没有查到体检人 在member表中插入一条体检人信息记录
                        member = new Member();
                        member.setIdcard(idCardStr);
                        member.setRegtime(new java.util.Date());
                        member.setSex((String)map.get("sex"));
                        member.setName((String)map.get("name"));
                        member.setPhonenumber((String)map.get("telephone"));
                        memberMapper.insert(member);

                    }
                    //生成一条预约记录 orders
                    Orders orders = new Orders();
                    orders.setMemberid(member.getId());
                    orders.setOrderstatus("未就诊");
                    orders.setOrdertype("微信预约");
                    orders.setSetmealid(setmealId);
                    orders.setOrderdate(date);
                    ordersMapper.insert(orders);
                    oid = orders.getId();
                    //修改该日期的已预约人数
                    ordersetting.setReservations(ordersetting.getReservations()+1);
                    ordersettingMapper.updateById(ordersetting);
                }
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return new Result(true,"预约成功",oid);
    }
    @Override
    public Map<String, Object>findOrderById(Integer id){
        Map<String, Object>map=new HashMap<>();
        try{
            Orders orders =ordersMapper.selectById(id);
            map.put("orderType",orders.getOrdertype());
            Date orderdate=orders.getOrderdate();
            String date= DateUtils.parseDate2String(orderdate);
            map.put("orderDate",date);
            Integer memberid=orders.getMemberid();
            Member member=memberMapper.selectById(memberid);
            map.put("member",member.getName());
            Integer setmealid=orders.getSetmealid();
            Setmeal setmeal=setmealMapper.selectById(setmealid);
            map.put("setmeal",setmeal.getName());
        }catch (Exception ex){
            ex.printStackTrace();
        }

        return map;

    }

}
